#! /bin/bash

# Create a instance and return the id if created instance

set -e
vultr_dir=$(dirname $(realpath $BASH_SOURCE))

function get_ssh_key () {
  vultr-cli ssh-key list \
    | awk '{ if (NF==1) exit;  if ( NR != 1 ) print "--ssh-keys " $1}' \
    | tr '\n' ' '
}

function main () {
  local readonly region=ewr;
  local readonly plan=vc2-1c-1gb;
  # 477     Debian 11 x64 (bullseye)        x64     debian
  local readonly os=477
  local readonly sshkeys=$(get_ssh_key)
  
  local readonly instance_id=$(vultr-cli instance create \
    --os $os \
    --plan $plan \
    --region $region \
    $sshkeys \
    | tee /dev/stderr \
    | awk ' /^ID/ { print $(NF); exit; }');

  # local readonly ip_addr=$($vultr_dir/wait-for-ip $instance_id);

  echo $instance_id;
}

main $@

